<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class product extends a_base {

    public $classpropertyid = 3;

    public function __construct() {
        parent::__construct();
        $this->load->model('m_product');
        $this->load->model('m_class');
    }

    public function index() {
        $orderkey_list = array('productid', 'classid', 'productname', 'smallpic', 'bigpic', 'keywords', 'description', 'details', 'isrecommend', 'clicknum', 'listid', 'adminid', 'addtime', 'isclose');
        $orderkey = $this->input->get('orderkey');
        if (!in_array($orderkey, $orderkey_list)) {
            $orderkey = 'productid';
        }
        $ascdesc = $this->input->get('ascdesc');
        if ($ascdesc != 'asc') {
            $ascdesc = 'desc';
        }
        $where = array();
        $productname = $this->input->get('productname');
        if (isset($_GET['productname']) && !empty($productname)) {
            $where['productname'] = $productname;
        }

        $isclose = $this->input->get('isclose');
        if (isset($_GET['isclose']) && $isclose != -1) {
            $where['isclose'] = $isclose;
        }
        $page = $this->input->get('page'); //当前页
        $page = intval($page);
        if ($page < 1) {
            $page = 1;
        }
        $cnt = $this->m_product->get_count($where);
        $page_size = 10;
        $arr = array(
            'page' => $page, //当前页
            'page_size' => $page_size, //分页总大小
            'total' => $cnt//总记录数
        );
        $start_page = ($page - 1) * $page_size;
        $data['pagination'] = new pagination($arr);

        $t_data = array(
            'where' => $where,
            'limit' => $page_size,
            'offset' => $start_page,
            'order' => $orderkey . ' ' . $ascdesc,
        );
        $list = $this->m_product->get_product_list($t_data);

        $this->load->model('m_productpic');
        foreach ($list as $k => $v) {
            $list[$k]['cnt'] = $this->m_productpic->get_count(array('productid' => $v['productid']));
        }
        $data['list'] = $list;

        $this->load->model('m_admin');
        $data['admin'] = $this->m_admin->get_all(array('column' => 'adminname,adminid'));

        $data['classlist'] = $this->m_class->get_all(array('column' => 'classname,classid'));
        $where['orderkey'] = $orderkey;
        $where['ascdesc'] = $ascdesc;
        $data['where'] = $where;
        $this->load->view('product/index', $data);
    }

    function move() {
        $productid = $this->input->get('productid');
        $list = explode('.', $productid);
        $productlist = $this->m_product->get_product_in_id($list);
        if (empty($productlist)) {
            // 提示
            $vt_data = array(
                'js_fun' => 'tips',
                'param' => array('请选择移动产品', 'index'),
            );
            $this->load->view('tips', $vt_data);
            return;
        }
        if (isset($_POST['btnSave'])) {
            $classid = intval($this->input->post('classid'));
            $gc_data = array(
                'column' => 'classid,classpropertyid,classname',
                'classid' => $classid,
                'classpropertyid' => $this->classpropertyid
            );
            $class = $this->m_class->get_class($gc_data);

            if (empty($class)) {
                // 提示
                $vt_data = array(
                    'js_fun' => 'tips',
                    'param' => array('请选择目标栏目', 'move?productid=' . $productid),
                );
                $this->load->view('tips', $vt_data);
                return;
            }
            foreach ($productlist as $k => $v) {
                $up_data = array(
                    'productid' => $v['productid'],
                    'classid' => $class['classid'],
                );
                $this->m_product->update($up_data);
            }
            redirect('product/index');
        }
        $cl_data = array(
            'classpropertyid' => $this->classpropertyid,
        );
        $v_data['tree'] = $this->m_class->get_class_tree($cl_data);

        $this->load->view('product/move', $v_data);
    }

    public function add() {
        $defalut = array(
            'classid' => 0,
            'productname' => '',
            'smallpic' => '',
            'bigpic' => '',
            'keywords' => '',
            'description' => '',
            'details' => '',
            'isrecommend' => 0,
            'clicknum' => 1,
            'listid' => 1,
            'addtime' => date('Y-m-d H:i:s'),
            'isclose' => 0
        );
        $productid = $this->input->get('productid');
        if ($productid > 0) {
            //修改
            $product = $this->m_product->get_product(array('productid' => $productid));
            if (empty($product)) {
                // 提示
                $vt_data = array(
                    'js_fun' => 'tips',
                    'param' => array('产品不存在', 'index'),
                );
                $this->load->view('tips', $vt_data);
                return;
            }
            $configid = $this->m_class->get_class(array('column' => 'configid', 'classid' => $product['classid']));
            $product['configid'] = $configid['configid'];
            $cl_data = array(
                'configid' => $configid['configid'],
                'classpropertyid' => $this->classpropertyid,
            );
            $data['classtree'] = $this->m_class->get_class_tree($cl_data);
            $data['post_url'] = b_url('product/add?productid=' . $productid);
            $data['product'] = $product;
        } else {
            $data['product'] = $defalut;
            $data['product']['configid'] = 0;
            $data['post_url'] = b_url('product/add');
        }
        if (isset($_POST['btnSave'])) {
            $post = $this->input->post();
            $result = array();
            foreach ($post as $k => $v) {
                if (isset($defalut[$k])) {
                    $defalut[$k] = $post[$k];
                }
            }
            if(!file_exists('.'.$defalut['smallpic'])){
                $defalut['smallpic']='';
            }
            if(!file_exists('.'.$defalut['bigpic'])){
                $defalut['bigpic']='';
            }
            unset($defalut['configid']);
            if ($productid > 0) {
                $defalut['productid'] = $product['productid'];
                $this->m_product->update($defalut);
            } else {
                $defalut['adminid'] = $this->session->userdata['user_id'];
                $defalut['addtime'] = date('Y-m-d H:i:s');
                $this->m_product->insert($defalut);
            }
            redirect('product/index');
            exit;
        }
        $this->load->model('m_config');
        $data['config'] = $this->m_config->get_all(array('column' => 'configid,languagever'));
        $this->load->view('product/add', $data);
    }

    function delete() {
        $productid = $this->input->get('productid');
        $list = explode('.', $productid);
        foreach ($list as $k => $v) {
            $v = intval($v);
            if ($v > 0) {
                $this->m_product->delete(array('productid' => $v));
            }
        }
        // 提示
        $vt_data = array(
            'js_fun' => 'tips',
            'param' => array('编号为' . str_replace('.', '，', $productid) . '产品删除成功!', 'index', 1),
        );
        $this->load->view('tips', $vt_data);
    }

    function openclose() {
        $productid = $this->input->get('productid');
        $status = $this->input->get('status');
        $str_status = '关闭';
        if ($status != 1) {
            $status = 0;
            $str_status = '开放';
        }
        $list = explode('.', $productid);
        foreach ($list as $k => $v) {
            $v = intval($v);
            if ($v > 0) {
                $this->m_product->update(array('productid' => $v, 'isclose' => $status));
            }
        }
        // 提示
        $vt_data = array(
            'js_fun' => 'tips',
            'param' => array('编号为' . str_replace('.', '，', $productid) . '产品' . $str_status . '成功!', 'index', 1),
        );
        $this->load->view('tips', $vt_data);
    }

    function get_class() {
        $configid = $this->input->get('configid');
        $data = array(
            'configid' => $configid,
            'classpropertyid' => $this->classpropertyid,
        );
        $list = $this->m_class->get_class_tree($data);
        echo json_encode($list);
    }

    function copy() {
        $productid = $this->input->get('productid');

        if(isset($_POST['btnSave'])) {
            $list = explode('.', $productid);
            $productlist = $this->m_product->get_product_in_id($list);
            $show=array();
            foreach ($productlist as $k => $v) {
                $show[]=$v['productid'];
                unset($v['productid']);
                $v['isrecommend']=0;
                $v['clicknum']=0;
                $v['listid']=0;
                $v['adminid'] = $this->session->userdata['user_id'];
                $v['addtime'] = date('Y-m-d H:i:s');
                $this->m_product->insert($v);
            }
            // 提示
            $vt_data = array(
                'js_fun' => 'tips',
                'param' => array('编号为'.  implode('，', $show).'的产品复制成功!', 'index'),
            );
            $this->load->view('tips', $vt_data);
            return;
        }
        
        $data = array(
            'classpropertyid' => $this->classpropertyid,
        );
        
        $cp_data['tree'] = $this->m_class->get_class_tree($data);
        
        $this->load->view('product/copy', $cp_data);
    }

}
